package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.LanHuoTable;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: lj
 * Date: 2019/5/27 14:02
 * Description: 揽货数据操作
 */
public class LanHuoTableDao extends BaseDao<LanHuoTable, Long> {

    public LanHuoTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), LanHuoTable.class);
    }

    /**
     * 查询运单是否需存在
     *
     * @param orderNo   订单号
     * @param waybillNo 运单号
     * @return
     */
    public List<LanHuoTable> queryByWaybillNo(long orderNo, Long waybillNo) {
        try {
            QueryBuilder<LanHuoTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("orderNo", orderNo);
            where.and().eq("waybillNo", waybillNo);
            List<LanHuoTable> list = queryBuilder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据运单号删除数据
     *
     * @param orderNo   订单号
     * @param waybillNo 运单号
     * @return
     */
    public int deleteByWaybillNo(Long orderNo, Long waybillNo) {
        try {
            QueryBuilder<LanHuoTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("orderNo", orderNo);
            where.and().eq("waybillNo", waybillNo);
            LanHuoTable lanHuoTable = queryBuilder.queryForFirst();
            return delete(lanHuoTable);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }
}
